package com.player.entity;

import javax.jdo.annotations.Index;
import javax.persistence.Id;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.Cached;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Indexed;
import com.googlecode.objectify.annotation.Unindexed;

@Entity
@Cached
public class Player {

	@Id
	Long id;

	// FIXME index/unindex (ifclauses... ??)
	@Index
	String playernameNormalized;
	@Unindexed
	String playername;

	@Indexed
	String email;

	@Unindexed
	String name;

	@Unindexed
	String prename;

	// FIXME use @embedded?
	// @Unindexed
	// Key<Ranking> ranking;

	public Player() {
	}

	public Player(String playername, String email, String name, String prename) {
		super();
		this.playername = playername;
		this.playernameNormalized = normalize(playername);
		this.email = email;
		// FIXME do i need name & prename?
		this.name = name;
		this.prename = prename;
	}

	public static String normalize(String s) {
		return s.trim().toUpperCase();
	}

	public Key<Player> getKey() {
		return new Key<Player>(Player.class, id);
	}

	public Long getId() {
		return id;
	}

	public String getPlayername() {
		return playername;
	}

	public String getPlayernameNormalized() {
		return playernameNormalized;
	}

	public String getEmail() {
		return email;
	}

	public String getName() {
		return name;
	}

	public String getPrename() {
		return prename;
	}

	@Override
	public String toString() {
		return "Player [id=" + id + ", email=" + email + ", playername=" + playername + ", prename=" + prename
				+ ", name=" + name + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((email == null) ? 0 : email.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((playername == null) ? 0 : playername.hashCode());
		result = prime * result + ((prename == null) ? 0 : prename.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Player other = (Player) obj;
		if (email == null) {
			if (other.email != null)
				return false;
		} else if (!email.equals(other.email))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (playername == null) {
			if (other.playername != null)
				return false;
		} else if (!playername.equals(other.playername))
			return false;
		if (prename == null) {
			if (other.prename != null)
				return false;
		} else if (!prename.equals(other.prename))
			return false;
		return true;
	}

}
